//
//  TShareView.m
//  Traceability
//
//  Created by ZhaoDongBo on 16/8/2.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "TShareView.h"
#import "TShareViewItem.h"

#import "Masonry.h"

@interface TShareView()

@property (nonatomic, strong) UIView * maskView;

@property (nonatomic, strong) UIView * bgContentView;

@property (nonatomic, strong) UIView * contentView;
@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) UIView * lineView;
@property (nonatomic, strong) TShareViewItem * item1;
@property (nonatomic, strong) TShareViewItem * item2;
@property (nonatomic, strong) TShareViewItem * item3;
@property (nonatomic, strong) TShareViewItem * item4;

@property (nonatomic, strong) UIButton * cancelButton;

@end

@implementation TShareView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void) setup {
    
    self.userInteractionEnabled = YES;
    
    [self addSubview:self.maskView];
    [self addSubview:self.bgContentView];
    
    [self.bgContentView addSubview:self.contentView];
    [self.bgContentView addSubview:self.cancelButton];
    
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.lineView];
    [self.contentView addSubview:self.item1];
    [self.contentView addSubview:self.item2];
    [self.contentView addSubview:self.item3];
    [self.contentView addSubview:self.item4];
    
    [self addAutoLayoutToView];
}

- (void)addAutoLayoutToView {
    
    WS(weakSelf);
    
    GET_SCREEN_SCALE(scale);
    GET_SCREEN_WIDTH(screen_width);
    
    const float itemWidth = (screen_width-20-160)/4.0;
    const float itemHeight = (screen_width-20-160)/4.0 + 20;
    
    [_maskView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [_contentView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-71);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(150*scale);
    }];
    
    [_titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(12);
    }];
    
    [_lineView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(42);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    [_item1 makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(20);
        make.top.mas_equalTo(weakSelf.contentView.mas_top).with.offset(60);
        make.size.mas_equalTo(CGSizeMake(itemWidth, itemHeight));
    }];
    
    [_item2 makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.item1.mas_top);
        make.leading.mas_equalTo(weakSelf.item1.mas_trailing).with.offset(40);
        make.size.mas_equalTo(CGSizeMake(itemWidth, itemHeight));
    }];
    
    [_item3 makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.item1.mas_top);
        make.leading.mas_equalTo(weakSelf.item2.mas_trailing).with.offset(40);
        make.size.mas_equalTo(CGSizeMake(itemWidth, itemHeight));
    }];
    
    [_item4 makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.item1.mas_top);
        make.leading.mas_equalTo(weakSelf.item3.mas_trailing).with.offset(40);
        make.size.mas_equalTo(CGSizeMake(itemWidth, itemHeight));
    }];
    
    [_cancelButton makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.bottom.mas_equalTo(-10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(51);
    }];
}


- (UIView *)maskView {
    
    if (!_maskView) {
        
        _maskView = [[UIView alloc] init];
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.userInteractionEnabled = YES;
        _maskView.alpha = 0;
        
        UITapGestureRecognizer *tapMaskView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareViewClose)];
        [_maskView addGestureRecognizer:tapMaskView];
    }
    
    return _maskView;
}

- (UIView *)bgContentView {

    if (_bgContentView == nil) {
        const float shareViewHeight = 45+(SCREEN_WIDTH-20-160)/4.0+10+51+20;
        _bgContentView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, shareViewHeight)];
    }
    
    return _bgContentView;
}

- (UIView *)contentView {
    
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = UIColorFromRGB(0xfdfdfd, 1);
        _contentView.layer.masksToBounds = YES;
        _contentView.layer.cornerRadius = 5;
    }
    
    return _contentView;
}

- (UILabel *)titleLabel {

    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text            = LZLocalizedString(@"share_to_friend", nil);
        _titleLabel.font            = [UIFont systemFontOfSize:12];
        _titleLabel.textColor       = UIColorFromRGB(0x999999, 1);
        _titleLabel.textAlignment   = NSTextAlignmentCenter;
    }
    
    return _titleLabel;
}

- (UIView *)lineView {

    if (_lineView == nil) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _lineView;
}

- (TShareViewItem *)item1 {
    
    if (_item1 == nil) {
        _item1 = [[TShareViewItem alloc] initWithTitle:LZLocalizedString(@"share_message", nil)
                                              andImage:[UIImage imageNamed:@"share_Messages"]];
        [_item1 setTag:TSHAREVIEW_MESSAGES_BUTTON_TYPE];
        [_item1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _item1;
}

- (TShareViewItem *)item2 {
    
    if (_item2 == nil) {
        _item2 = [[TShareViewItem alloc] initWithTitle:LZLocalizedString(@"share_email", nil)
                                              andImage:[UIImage imageNamed:@"share_Mail"]];
        [_item2 setTag:TSHAREVIEW_MAIL_BUTTON_TYPE];
        [_item2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _item2;
}

- (TShareViewItem *)item3 {
    
    if (_item3 == nil) {
        _item3 = [[TShareViewItem alloc] initWithTitle:LZLocalizedString(@"share_facebook", nil)
                                              andImage:[UIImage imageNamed:@"share_Facebook"]];
        [_item3 setTag:TSHAREVIEW_FACEBOOK_BUTTON_TYPE];
        [_item3 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _item3;
}

- (TShareViewItem *)item4 {
    
    if (_item4 == nil) {
        _item4 = [[TShareViewItem alloc] initWithTitle:LZLocalizedString(@"share_twitter", nil)
                                              andImage:[UIImage imageNamed:@"share_Twitter"]];
        [_item4 setTag:TSHAREVIEW_TWITTER_BUTTON_TYPE];
        [_item4 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _item4;
}

- (UIButton *)cancelButton {
    
    if (_cancelButton == nil) {
        _cancelButton = [[UIButton alloc] init];
        _cancelButton.backgroundColor       = UIColorFromRGB(0xfdfdfd, 1);
        _cancelButton.layer.masksToBounds   = YES;
        _cancelButton.layer.cornerRadius    = 5;
        
        [_cancelButton setTitle:LZLocalizedString(@"cancel", @"") forState:UIControlStateNormal];
        [_cancelButton setTitleColor:UIColorFromRGB(0x33a928, 1) forState:UIControlStateNormal];
        [_cancelButton setTitleColor:UIColorFromRGB(0x33a928, 1) forState:UIControlStateHighlighted];
        
        [_cancelButton addTarget:self action:@selector(shareViewClose) forControlEvents:UIControlEventTouchUpInside];

    }
    
    return _cancelButton;
}


- (void)buttonAction:(UIButton *)sender {
    
    if (_shareViewButtonActionBlock) {
        _shareViewButtonActionBlock(sender.tag);
    }
}

- (void)shareViewOpen
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];

    WS(weakSelf);
    const float shareViewHeight = 45+(SCREEN_WIDTH-20-160)/4.0+10+51+20;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        weakSelf.maskView.alpha = 0.3f;
        
        CGRect frame    = weakSelf.bgContentView.frame;
        frame.origin.y  = SCREEN_HEIGHT-shareViewHeight;
        weakSelf.bgContentView.frame = frame;
        
    } completion:^(BOOL finished) {
        
    }];
}

- (void)shareViewClose
{
    WS(weakSelf);
    
    [UIView animateWithDuration:0.3 animations:^{
        
        weakSelf.maskView.alpha = 0.0f;
        
        CGRect frame    = weakSelf.bgContentView.frame;
        frame.origin.y  = SCREEN_HEIGHT+40;
        weakSelf.bgContentView.frame = frame;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
}


@end
